﻿using System;
using System.Windows;
using Kiva7.DataClasses;
using Kiva7.Translations;
using Kiva7.DataRequests;
using Kiva7.ViewModels;
using Kiva7.Images;
using Kiva7.Utilities;

namespace Kiva7.Pages
{
    public partial class FriendEditPage
    {
        private const string Url = "/Pages/FriendEditPage.xaml";
        public static readonly Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        public FriendEditPage()
        {
            InitializeComponent();
            CreateStandardControls();
            request = new LenderRequest();

            DataContext = request;
        }

        public static Uri GetPageUrl(Lender lender)
        {
            if (lender == null)
                return null;

            UrlCreator url = new UrlCreator(Url);
            url.Add("lenderId", lender.LenderId);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }

        public string LenderId
        {
            get
            {
                return NavigationContext.GetQueryString("lenderId");
            }
        }

        private void BasePhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LenderID.Text = LenderId;
        }


        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.Delete, ImageUrls.Delete, Delete_Click);
            AddBarButton(AppResources.Save, ImageUrls.Save, Save_Click);
        }

        
        LenderRequest request;

        private void LenderID_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            request.GetLender(LenderID.Text);
        }

        private void Save_Click(object sender, EventArgs e)
        {
            if (DataController.ViewModel.Friends.Edit(LenderId,
                new Friend()
                {
                    LenderId = LenderID.Text
                }))
            {
                
            }
            NavigationService.GoBack();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (DataController.ViewModel.Friends.Delete(LenderId))
            {
                
            }
            NavigationService.GoBack();
        }

    }
}